const db = require('../config/db'); // 导入 db 模块

const markMessageAsRead = (req, res) => {
  const { messageIds } = req.body; // 接收消息 ID 数组

  if (!messageIds || !Array.isArray(messageIds)) {
    return res.status(400).json({ error: 'messageIds must be an array' });
  }

  const sql = `
    UPDATE messages 
    SET is_read = TRUE 
    WHERE id IN (${messageIds.map(() => '?').join(',')})`;
  
  db.run(sql, messageIds, function(err) {
    if (err) {
      return res.status(500).json({ error: 'Database error' });
    }
    res.json({ message: 'Messages marked as read', updatedCount: this.changes });
  });
};

module.exports = { markMessageAsRead }; 